// pages/profile/profile.js
Page({
  data: {
    userInfo: null,
    baseUrl: 'http://localhost:3000'
  },

  onLoad: function() {
    console.log('Profile page loaded');
    this.checkLogin();
    this.loadUserInfo(); // 确保在页面加载时调用 loadUserInfo
  },

  onShow: function() {
    console.log('Profile page shown');
    this.loadUserInfo(); // 确保每次页面显示时都调用 loadUserInfo
  },

  // 检查登录状态
  checkLogin: function() {
    const userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
      console.log('No user info found in storage, redirecting to login');
      wx.redirectTo({
        url: '/pages/login/login'
      });
    } else {
      console.log('User info found in storage:', userInfo);
    }
  },

  // 加载用户信息
  loadUserInfo: function() {
    const userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      console.log('Setting user info to state:', userInfo);
      this.setData({
        userInfo
      }, () => {
        console.log('Data updated with user info:', this.data.userInfo);
      });
    } else {
      console.log('Failed to get user info from storage');
      wx.showToast({
        title: '用户信息加载失败',
        icon: 'none'
      });
    }
  },

  // 导航到我的岗位
  navigateToMyJobs: function() {
    wx.switchTab({
      url: '/pages/jobs/jobs'
    });
  },

  // 导航到岗位订阅
  navigateToSubscriptions: function() {
    wx.navigateTo({
      url: '/pages/subscriptions/subscriptions'
    });
  },

  // 导航到收藏的岗位
  navigateToFavorites: function() {
    wx.navigateTo({
      url: '/pages/favorites/favorites'
    });
  },

  // 导航到设置
  navigateToSettings: function() {
    wx.navigateTo({
      url: '/pages/settings/settings'
    });
  },

  // 导航到发布新岗位（仅对企业用户显示）
  navigateToPublishJob: function() {
    wx.navigateTo({
      url: '/pages/addjob/addjob'
    });
  },

  // 退出登录
  handleLogout: function() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          wx.clearStorageSync();
          wx.redirectTo({
            url: '/pages/login/login'
          });
        }
      }
    });
  }
});